﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FCTC_Web.Models;
using FCTC_Web.DAL;
using FCTC_Web.Utilities;
using DAL;
using System.Data.Objects;

namespace FCTC_Web.Controllers.Maintains
{
    public class TeamController : Controller
    {
        //[Authorize]
        public ViewResult Index(int? pageId)
        {
            return View("Index", Pager6.Ins.GetPage<Team>(pageId));
        }

        [HttpPost]
        public ActionResult Edit(Team team)
        {
            if (ModelState.IsValid)
            {
                CH.Edit<Team>(team);
                var pageId = Pager6.Ins.GetPageId<Team>(team.ID);
                return RedirectToAction("Index", new { pageId = pageId });
            }
            return View(team);
        }

        [HttpGet]
        public PartialViewResult MemberCreate()
        {
            return PartialView(@"~\views\team\partial\memberchange.cshtml");
        }

        [HttpGet]
        public PartialViewResult MemberUpdate(string teamid, string memberid)
        {
            var teamMemberID = Convert.ToInt32(memberid);
            return PartialView(@"~\views\team\partial\memberchange.cshtml", CH.GetDataById<TeamMember>(teamMemberID));
        }

        [HttpGet]
        public PartialViewResult GetTeamSource(string condition)
        {
            return PartialView(@"~\views\shared\controls\teamsource.cshtml", CH.GetAllData<Team>(i => i.FullName.Contains(condition)
                || i.ContactNumber == condition || i.TeamContactor.Contains(condition)));
        }

        [HttpPost]
        public PartialViewResult MemberUpdate(string teamid, string memberid, string memberFullName, string gender, string identity, string memberStatus, string hasVehicle, string sequense)
        {
            var teamIdentity = Convert.ToInt32(teamid);
            var vehicle = Convert.ToBoolean(hasVehicle);
            int? s;
            if (string.IsNullOrWhiteSpace(sequense))
                s = null;
            else
                s = Convert.ToInt32(sequense);

            TeamMember tm;
            if (string.IsNullOrEmpty(memberid))
                tm = new TeamMember();
            else
            {
                var teammberID = Convert.ToInt32(memberid);
                tm = CH.GetDataById<TeamMember>(teammberID);
            }

            tm.TeamID = teamIdentity;
            tm.MemberFullName = memberFullName;
            tm.Gender = gender;
            tm.Identity = identity;
            tm.MemberStatus = memberStatus;
            tm.HasVehicle = vehicle;
            tm.Sequense = s;
            tm.Team = null;
            if (string.IsNullOrEmpty(memberid))
                CH.Create<TeamMember>(tm);
            else
                CH.EditWithContextRepository<TeamMember>(tm);

            return PartialView(@"~\views\team\partial\MemberList.cshtml", CH.GetDataById<Team>(teamIdentity).Members.OrderByDescending(i => i.Sequense).ToList());
        }

        [HttpPost]
        public PartialViewResult MemberDelete(string teamid, string memberid)
        {
            var teamIdentity = Convert.ToInt32(teamid);

            if (!string.IsNullOrEmpty(memberid))
            {
                var teammberID = Convert.ToInt32(memberid);
                try
                {
                    CH.DeleteWithContextRepository<TeamMember>(teammberID);
                }
                catch(Exception e)
                { 
                }
            }
            return PartialView(@"~\views\team\partial\MemberList.cshtml", CH.GetDataById<Team>(teamIdentity).Members.OrderByDescending(i => i.Sequense).ToList());
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            CH.Delete<Team>(id);
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult Create(Team team)
        {
            if (ModelState.IsValid)
            {
                team.UpdateTime = DateTime.Now;
                team.UpdatedUser = HttpContext.User.Identity.Name;
                CH.Create<Team>(team);
                var pageId = Pager6.Ins.GetPageId<Team>(team.ID);
                return RedirectToAction("Index", new { pageId = pageId });
            }
            return View(team);
        }

        public ViewResult Details(int? id)
        {
            Team team = CH.GetDataById<Team>(id);
            return View(team);
        }

        public ActionResult Create()
        {
            return View();
        }

        public ActionResult Edit(int? id)
        {
            return View(CH.GetDataById<Team>(id));
        }

        public ActionResult Delete(int? id)
        {
            Team team = CH.GetDataById<Team>(id);
            return View(team);
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }
}